package com.baizhi.controller;

import com.baizhi.domain.dto.CommentSaveDto;
import com.baizhi.domain.mongo.Comment;
import com.baizhi.service.CommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("comment")
public class CommentController {
    private static final Logger log = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private CommentService commentService;

    // 查看文章的评论列表
    @GetMapping("/aid/{aid}")
    public ResponseEntity<List<Comment>> findList(@PathVariable("aid") Integer aid) {
        log.info("comment list ...");
        log.info("aid : {}", aid);
        List<Comment> list = commentService.findCommentsByAid(aid);
        return ResponseEntity.ok(list);
    }

    // 添加评论
    @PostMapping("save")
    public ResponseEntity<Boolean> saveComment(@Validated @RequestBody CommentSaveDto param) {
        log.info("comment save ...");
        log.info("param : {}", param);
        commentService.saveComment(param);
        return ResponseEntity.ok(true);
    }

    // 删除评论
    @DeleteMapping("{id}")
    public ResponseEntity<Boolean> deleteComment(@PathVariable("id") String id) {
        log.info("comment delete ...");
        log.info("id : {}", id);
        commentService.deleteByCid(id);
        return ResponseEntity.ok(true);
    }
}
